<?php
namespace App\Helpers;
use app\libs\TencentIm\TimRestAPI;

/**
 * Created by PhpStorm.
 * Author: Wudean
 * Date: 2018/5/30 0030
 * Time: 14:14
 * Name: 腾讯云 通信 IM
 */
use Illuminate\Support\Facades\Config;

class TencentIm
{
    protected $api;
    protected $sdkappid;
    protected $identifier;
    protected $private_key_path;
    protected $signature;
    protected $usersig='';
    public function __construct()
    {
        $this->sdkappid = 1400046204;
        $this->identifier = "admin";
        $this->private_key_path = Config::get('constants.PARIVATE_PEM_PATH');
        $this->signature = Config::get('constants.SIGNATURE');
        $this->api= new TimRestAPI();

        $this->api->init($this->sdkappid,$this->identifier);
    }

    /**
     * @param $account_id 发送者
     * @param $receiver 接受者
     * @param $msg_conten 内容
     * @name 发送消息单条消息
     */
    public function sendMsg($account_id, $receiver, $msg_conten){
        $ret = $this->api->openim_send_msg("myadmin", "lilei", "hello");
    }

    /**
     * @param $account_id 发送者
     * @param $group_id 群组id
     * @param $text_content 内容
     * @name 群组消息
     * @return bool
     */
    public function groupSendGroupMsg($account_id, $group_id, $text_content){
        $ret = $this->api->group_send_group_msg($account_id, $group_id, $text_content);
        if($ret['ErrorCode']==0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * @param $group_id
     * @param $text_content
     * @param $receiver_id
     * @return bool
     * @name 群发系统消息
     */
    public function groupSystemMsg($group_id, $text_content, $receiver_id){
        $ret = $this->api->group_send_group_system_notification($group_id, $text_content, $receiver_id);
        if($ret['ErrorCode']==0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * @param $group_id 分组
     * @param int $limit 条数
     * @param int $offset 页码
     * @return array|bool|mixed|string
     * @name 获取群组成员详细资料
     */
    public function getGroupMemberInfo($group_id, $limit=15, $offset=1){
        $ret = $this->api->group_get_group_member_info($group_id, $limit, $offset);
        if($ret['ErrorCode']==0){
            return $ret;
        }else{
            return false;
        }
    }

    /**
     * 获取云通信IM sig
     * @return string
     */
    public function userSig(){
        $sig = $this->api->generate_user_sig($this->identifier, '86400',$this->private_key_path, $this->signature);
        return $sig;
    }


}
